package chase.myrewards;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.rdbms.AppEngineDriver;
import com.google.appengine.api.utils.SystemProperty;

import chase.myrewards.beans.Account;
import chase.myrewards.beans.AccountList;
import chase.myrewards.beans.Customer;

/**
 * Servlet that takes customer id as input and finds all the accounts
 * belonging to that customer and returns the generated HTML page.
 * 
 * @author Meena Abdou
 * @date April 21, 2013
 * @author Archi Pandurangi
 * @date April 23, 2013
 * Added database code
 */

public class AccountListServlet extends HttpServlet implements Serializable {

	/**
	 * This number must be unique for every class. And it can never change.
	 * You have to generate it automatically by clicking on the class name and
	 * then hitting Ctrl+1.  Then click on Add generated serial version ID. Then
	 * copy these comments on top of the generated id.
	 * 
	 * @serial serialVersionUID
	 */
	private static final long serialVersionUID = 6394668812396677599L;
	
	private static final Logger log = Logger.getLogger(AccountListServlet.class.getName());
	
	private Customer cust; // Added by Archi on May 9th
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String address = "/WEB-INF/jsp/accountlist.jsp";
		AccountList accountList = new AccountList();
		cust = null;
		
		log.info("Session Id is " + request.getSession().getId());

		//Added by Archi
		//Making connection with the database and querying for account list for the given customer ID
		if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
			Connection c = null;
			try {
				DriverManager.registerDriver(new AppEngineDriver());
				c = DriverManager.getConnection("jdbc:google:rdbms://myrewards2013:myrewardsdatabase/myrewards");

				
				java.sql.Statement stmt =  c.createStatement();
				// Added by Archi on May 9th
				//getting the session object customer
				cust = (Customer) request.getSession().getAttribute("customer");

				//getting customer id for the session
				String id = cust.getCustomerID();
				
			//	String custid = request.getParameter("customerID"); //Getting this from client, still needs to be resolved
				
				java.sql.ResultSet rs = stmt.executeQuery(Queries.ACCOUNT_LIST(id));

				// Loading the resulting data from the query into accountlist
				while(rs.next()){
					Account a = new Account();
					a.setAccountNum(rs.getString("account_num"));
					a.setAccountType(rs.getString("account_type"));	
					accountList.insertAccount(a);
				}

				HttpSession session = request.getSession();
				session.setAttribute("accountlist", accountList);

				RequestDispatcher dispatcher = request.getRequestDispatcher(address);
				dispatcher.forward(request, response);
			} 
			catch (SQLException e) {
				log.log(Level.SEVERE, "SQLException", e);
			}
			finally {
				if (c != null){ 
					try {
						c.close();
					} 
					catch (SQLException ignore) {}
				}
			} // End of try-catch block
		}

		else{
			// Since we don't have database code yet, I have just created some mock accounts
			Account a1 = new Account();
			a1.setAccountNum("000000000000");
			a1.setAccountType("sapphire");
			Account a2 = new Account();
			a2.setAccountNum("000000000001");
			a2.setAccountType("slate");
			Account a3 = new Account();
			a3.setAccountNum("000000000002");
			a3.setAccountType("freedom");

			accountList.insertAccount(a1);
			accountList.insertAccount(a2);
			accountList.insertAccount(a3);
			
			HttpSession session = request.getSession();
			
			Customer cust = (Customer) session.getAttribute("customer");
			log.info(cust.getCustomerID());
			
			session.setAttribute("accountlist", accountList);

			RequestDispatcher dispatcher = request.getRequestDispatcher(address);
			dispatcher.forward(request, response);
		}
		
		return;
	}
}
